package dao.daocontrollers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import api.entitites.IGarage;
import api.entitites.IMaster;
import api.entitites.IOrder;
import entitiesfactory.EntitiesFactory;

public class EntitiesGenerator {

	private static EntitiesFactory entitiesFactory = EntitiesFactory.getInstance();
	
	
	
	private static IOrder createOrder(ResultSet orderResult) throws SQLException{
		IOrder order = (IOrder) entitiesFactory.getObject(IOrder.class);
		int number = orderResult.getInt(2);
		String orderStatus = orderResult.getString(3);
		Date fillingDate = orderResult.getDate(4);
		Date beginingDate = orderResult.getDate(5);
		Date deadlineDate = orderResult.getDate(6);
		int price = orderResult.getInt(7);
		int masterId = orderResult.getInt(8);
		int garageId = orderResult.getInt(9);
		
		order.setNumberOrder(number);
		if (orderStatus != null) {
			order.setOrderStatus(orderStatus);
		}
		order.setFillingDate(fillingDate);
		order.setBeginingDate(beginingDate);
		order.setDeadlineDate(deadlineDate);
		order.setPrice(price);
		order.sendMasterGarage(masterId, garageId);
		return order;
	}

	
	private static IMaster createMaster(ResultSet masterResult) throws SQLException{
		IMaster master = (IMaster) entitiesFactory.getObject(IMaster.class);
		String masterName = masterResult.getString(1);
		boolean free = masterResult.getBoolean(2);
		master.setNameMaster(masterName);
		if (!free) {
			master.markMaster();
		}
		return master;
		
	}
	
	
	static IOrder parserOrder(ResultSet orderFromDbResult) throws SQLException {

		orderFromDbResult.next();
		IOrder order = createOrder(orderFromDbResult);		
		orderFromDbResult.getStatement().close();
		orderFromDbResult.close();
		
		return order;

	}

	static IMaster parserMaster(ResultSet masterFromDbResult)
			throws SQLException {
		masterFromDbResult.next();
		IMaster master = createMaster(masterFromDbResult);
		masterFromDbResult.close();

		return master;
	}
	

	static IGarage parserGarage(ResultSet garageFromDbResult)
			throws SQLException {
		IGarage garage = (IGarage) entitiesFactory.getObject(IGarage.class);
		while (garageFromDbResult.next()) {
			int number = garageFromDbResult.getInt(2);
			garage.setNumberGarage(number);
			boolean empty = garageFromDbResult.getBoolean(3);
			if (!empty) {
				garage.markGarageNotEmpty();
			}
		}
		garageFromDbResult.close();

		return garage;
	}

	static java.sql.Date conversionDate(Date d) {
		java.sql.Date date = new java.sql.Date(d.getTime());
		return date;
	}
	

	static ArrayList<IOrder> parserOrders(ResultSet sortOrdersResult)
			throws SQLException {
		ArrayList<IOrder> ordersList = new ArrayList<IOrder>();
		while (sortOrdersResult.next()) {
			IOrder order=createOrder(sortOrdersResult);
			ordersList.add(order);
			
		}
		sortOrdersResult.close();
		return ordersList;
	}

	
	static ArrayList<IMaster> parserMasters(ResultSet sortMastersResult)
			throws SQLException {
		ArrayList<IMaster> mastersList = new ArrayList<IMaster>();
		while (sortMastersResult.next()) {
		IMaster master=createMaster(sortMastersResult);
			mastersList.add(master);
		}
		sortMastersResult.close();
		return mastersList;

	}

}
